import axios from 'axios'
import { message, Modal, } from 'ant-design-vue';

// import router from '../router/index'

// 默认配置
// axios.defaults.withCredentials = false
// axios.defaults.timeout = 50000
// axios.defaults.headers = { 'X-Requested-With': 'XMLHttpRequest' }
axios.defaults.baseURL = 'http://192.168.18.118:9103'  //process.env.API_ROOT


// 请求拦截器
axios.interceptors.request.use(
    config => {
        if (config.method === 'get') {
        }
        return config
    },
    err => {
        message.error('请求错误');
        return Promise.reject(err)
    }
)
// 响应拦截器
axios.interceptors.response.use(
    response => {
        // 请求成功的时候更新token
        // const token = response.headers["authorization"];  // 需要服务器设置 'Access-Control-Expose-Headers' : 'Authorization',
        const token = response.headers["token"];
        if (token) {
            localStorage.setItem('token', token);
        }
        return response
    },
    error => {
        if (error.response && error.response.status === 401) {
            Modal.error({
                title: '提示：',
                content: '登录信息过期，请重新登录',
                okText: '重新登录',
                onOk: () => {
                  window.location.reload()
                  router.push({path: '/login',})
                }
              })
            console.log(error.response)
        } else {
            if(error.response.status === 500){
                message.error(
                    '服务器请求出现错误，请及时检查!',
                    10,
                  );
            }else{
                message.error(
                    '内部其他错误!',
                    10,
                  );
            }
            console.log(error.response)
        }
        
        return Promise.reject(error)
    }
)
// 错误处理
function checkStatus(response) {
    return new Promise((resolve, reject) => {
        if (response.status === 200) {
            resolve(response.data)
        } else {
            reject(response.data)
        }
    })
}

export default {
    get(url, params) {
        return axios({
            method: 'get',
            url,
            params,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
                // 'Authorization': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    post(url, data) {
        return axios({
            method: 'post',
            url,
            data,
            headers: {
                // 'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/json;charset=UTF-8',
                'token': `${localStorage.getItem('token')}`
            }
        }).then(checkStatus)
    },
    put(url, data) {
        return axios({
            method: 'put',
            url,
            data
        }).then(checkStatus)
    },
    putParams(url, params) {
        return axios({
            method: 'put',
            url,
            params
        }).then(checkStatus)
    },
    putForm(url, data) {
        return axios({
            method: 'put',
            url,
            data,
            headers: {
                'X-Requested-With': 'XMLHttpRequest',
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
            }
        })
    },
    delete(url, params) {
        return axios({
            method: 'delete',
            url,
            params
        }).then(checkStatus)
    }

}

